package com.example.gaojt.androiddemo.boostDemo;

import android.content.Context;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;

/**
 * Created by gaojt on 7/26/17.
 */

public class BoostApi {

    private static final boolean DEBUG = true;
    private static final String TAG = "BoostApi";
    public static final int SKIP_BYTE_COUNT = 2;
    public static final String ENCODING = "utf-8";

    public void aa(Context context) {
        InputStream inputStream = null;
        ByteArrayInputStream byteArrayIS = null;
        try {
            // FIXME
            inputStream = context.getAssets().open("junkprocess_en_1.0.3.filter");
            int len = inputStream.available() - SKIP_BYTE_COUNT;
            if (len > 0) {
                byte[] content = new byte[len];
                inputStream.skip(SKIP_BYTE_COUNT);
                int nReadCount = inputStream.read(content);
                if (nReadCount == len) {
                    for (int nIdx = 0; nIdx < len; nIdx++) {
                        content[nIdx] = (byte) (~(content[nIdx] ^ (byte) 0x8F));
                    }
                    byteArrayIS = new ByteArrayInputStream(content);
                    load((new InputStreamReader(byteArrayIS, ENCODING)));
                }
            }
        } catch (Exception e) {
            if (DEBUG) {
                Log.w(TAG, "aa: ", e);
            }
        } finally {
            IOUtils.closeSilently(inputStream);
            IOUtils.closeSilently(byteArrayIS);
        }
    }

    private static class Section {
        public Collection<String> keyList = new LinkedList<String>();
        public Map<String, String> keyValueMap = new ArrayMap<String, String>();
    }

    public boolean load(Reader reader) {
        Map<String, Section> mSectionMap = new ArrayMap<String, Section>();
        Collection<String> mSectionList = new LinkedList<String>();
        try {

            String line = null;
            Section section = null;
            BufferedReader is = new BufferedReader(reader);

            while ((line = is.readLine()) != null) {
                line = line.trim();
                if (TextUtils.isEmpty(line)) {
                    continue;
                }

                switch (line.charAt(0)) {
                    // comments
                    case ';':
                    case '#':
                        break;

                    // section
                    case '[':
                        if (line.length() <= 2 || line.charAt(line.length() - 1) != ']') {
                            if (DEBUG) {
                                throw new Exception("invalid section name");
                            }
                        }

                        String sectionName = line.substring(1, line.length() - 1);
                        Log.d(TAG, "load: sectionName = " + sectionName);
                        if (!mSectionMap.containsKey(sectionName)) {
                            section = new Section();
                            mSectionList.add(sectionName);
                            mSectionMap.put(sectionName, section);
                        }
                        break;

                    // key-value
                    default:
                        if (section == null) {
                            if (DEBUG) {
                                throw new Exception("not found section name");
                            }
                        }

                        int index = line.indexOf('=');
                        if (index == -1) {
                            if (DEBUG) {
                                throw new Exception("invalid key-value format");
                            }
                        }

                        String key = line.substring(0, index);
                        String value = line.substring(index + 1, line.length());
                        Log.d(TAG, "load: key = " + key + ",value = " + value);

                        section.keyList.add(key);
                        section.keyValueMap.put(key, value);
                        break;
                }
            }
            return true;
        } catch (Exception e) {
            if (DEBUG) {
                Log.d(TAG, "load: ");
            }
            return false;
        }
    }

    /**
     * Created by gaojt on 7/26/17.
     */

    public static class IOUtils {
        public static void closeSilently(Closeable closeable) {
            if (closeable == null) return;
            try {
                closeable.close();
            } catch (Exception e) {}
        }

    }
}
